package com.atguigu.gmall.product.schedul;

import com.atguigu.gmall.common.cache.BloomType;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.controller.BloomFilterAddDataController;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author dyq
 * @description
 * @data 27/1/2022
 */
@Slf4j
@Configuration
@EnableScheduling
public class StaticScheduleTask {

    @Qualifier(BloomType.SKU)
    @Autowired
    RBloomFilter<Object> skuFilter;

    @Autowired
    SkuInfoService skuInfoService;

    //添加定时任务
    @Scheduled(cron = "0 0 2 * * ? ")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void bloomAddAll(){
        log.info("执行定时任务，重新创建布隆过滤器");
        //删除旧布隆，redis中的数据就删除了
        skuFilter.delete();
        //1、（可以这么做）服务器时间判断，不是晚上不允许重置
        skuFilter.tryInit(1000000L,0.001);
        List<Long> skuIds = skuInfoService.getAllId();
        //添加新布隆
        skuIds.stream().forEach(item -> skuFilter.add(RedisConst.SKUKEY_PREFIX + item + RedisConst.SKUKEY_SUFFIX));
    }
}
